package com.ruoyi.business.controller;

import com.ruoyi.business.domain.ClassInfo;
import com.ruoyi.business.domain.bo.ClassInfoBo;
import com.ruoyi.business.domain.vo.ClassInfoVo;
import com.ruoyi.business.domain.vo.UserVo;
import com.ruoyi.business.service.IClassInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.sun.net.httpserver.HttpContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/9/2
 **/
@RestController
@RequestMapping("/business/classInfo")
public class ClassInfoController extends BaseController {

    @Autowired
    private IClassInfoService classInfoService;

    /**
     * 分页查询班级列表信息
     * @param bo
     * @return
     */
    @ApiOperation("分页查询班级列表信息")
    @GetMapping("/list")
    public AjaxResult queryClassList(ClassInfoBo bo) {
        TableDataInfo<ClassInfoVo> result = classInfoService.queryClassListByPage(bo);
        return AjaxResult.success(result);
    }

    @ApiOperation("新增班级信息")
    @PostMapping
    public AjaxResult insertClassInfo(@RequestBody ClassInfoBo bo) {
        classInfoService.insertClassInfo(bo);
        return AjaxResult.success();
    }

    @ApiOperation("修改班级信息")
    @PutMapping
    public AjaxResult updateClassInfo(@RequestBody ClassInfoBo bo) {
        classInfoService.updateClassInfo(bo);
        return AjaxResult.success();
    }

    @ApiOperation("删除班级基本信息")
    @DeleteMapping("/{id}")
    public AjaxResult deleteClassInfo(@PathVariable("id") String id) {
        classInfoService.deleteClassInfo(id);
        return AjaxResult.success();
    }


    @ApiOperation("获取班级基本信息")
    @GetMapping
    public AjaxResult getClassInfo(@RequestParam("id") String id) {
        ClassInfoVo vo = classInfoService.getClassInfo(id);
        return AjaxResult.success(vo);
    }

    @ApiOperation("导出班级成员信息")
    @GetMapping("/export/{id}")
    public void exportClassMemberInfo(@PathVariable("id") String id,
                                      HttpServletResponse response) throws UnsupportedEncodingException {
        // 查询班级成员名单
        List<UserVo> list = classInfoService.queryMemberByClassId(id);
        ClassInfo classInfo = classInfoService.getById(id);
        list = list.stream().map(item -> {
            item.setMyClass(classInfo.getClassName());
            return item;
        }).collect(Collectors.toList());
        // 翻译班级成员名单
        response = setResponse(list, response, classInfo.getClassName());
        ExcelUtil<UserVo> excelUtil = new ExcelUtil<UserVo>(UserVo.class);
        excelUtil.exportExcel(response, list, classInfo.getClassName());
    }


    /**
     * 设置响应信息
     * @param response
     * @return
     */
    private HttpServletResponse setResponse(List<UserVo> list, HttpServletResponse response, String filename) throws UnsupportedEncodingException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"class.xlsx\"");
        response.setContentType("application/octet-stream; charset=UTF-8");
        return response;
    }

    /**
     * 分页查询课程关联对象
     */
    @ApiOperation("查询课程关联对象")
    @GetMapping("/refTable")
    public AjaxResult refTable(@NotEmpty(message = "关联对象不能为空") String refColumn, Integer pageSize, Integer currentPage, String filterContent, String filterId) {
        return AjaxResult.success(classInfoService.selectRefTableByName(refColumn, pageSize, currentPage, filterContent, filterId));
    }

}
